package com.witte.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.witte.entity.Chance;
import com.witte.entity.RequestBean;
import com.witte.entity.RespBean;
import com.witte.entity.Service;
import com.witte.entity.User;
import com.witte.service.ChanceService;
import com.witte.service.UserService;

@Controller
@ResponseBody
@RequestMapping("/api")
@CrossOrigin
public class ChanceController {

	@Autowired
	private ChanceService chanceService;
	
	@GetMapping(value = "/finAll")
	public RespBean begin() {
		RespBean respBean =RespBean.ok(null, chanceService.selectAll());
		return respBean ;
	}
	
	@PostMapping(value = "/selectKey")
	public RespBean selectKey(@RequestBody RequestBean key) {
		RespBean respBean =RespBean.ok(null, chanceService.selectKey(key.getStr()));
		return respBean ;
	}
	
	@PostMapping(value = "/saveChance")
	public RespBean save(@RequestBody Chance chance) {
		RespBean respBean =RespBean.ok(null, chanceService.updateByChanceNo(chance));
		return respBean ;
	}
	
	@PostMapping(value = "/deleteChance")
	public RespBean delect(@RequestBody Chance chance) {
		RespBean respBean =RespBean.ok(null, chanceService.deleteByPrimaryKey(chance.getChanceno()));
		return respBean ;
	}
	
	@PostMapping(value = "/insertChance")
	public RespBean insert(@RequestBody Chance chance) {
		RespBean respBean =RespBean.ok(null, chanceService.insertSelective(chance));
		return respBean;
	}
	
	@PostMapping("/chancebelong")
	public RespBean updateAllot(@RequestBody Chance chance) {
		int rs=chanceService.chancebelong(chance);
		RespBean respBean=RespBean.error("新加错误！");
		if(rs>0)
			respBean=RespBean.ok("ok");
		return respBean;
	}
	
	@GetMapping("/getStateList")
	public RespBean getStateList(Integer beginPage,Integer pageSize){
		List<Service> list=chanceService.getStateList(beginPage,pageSize);
		RespBean respBean=RespBean.error("新加错误！");
		if(list!=null) {
			respBean=RespBean.ok("ok",list);
			respBean.setTotleSize(chanceService.getRecodeCount());
		}
		return respBean;
	}
	
	
	 @GetMapping(value = "/muchDel")
	    public RespBean deleteNos(String nos) {
	        String[] chanceNos= nos.split(",");
	        RespBean respBean = null;
//	        RespBean respBean =RespBean.ok(null, cusService.deleteByPrimaryKey(cusNos.getNo()));
	        for(int i=0; i<chanceNos.length; i++) {
	            respBean = RespBean.ok(null, chanceService.deleteByPrimaryKey(Integer.parseInt(chanceNos[i])));
	        }
	        return respBean;
	    }
//	public HttpResult delete(@RequestParam String ids) {
//		String[] ids= ids.split(",");
//		system.out.println(ids)
//	}
	

}
